//
//  Ext + UIButton.swift
//  SinaProjectTest
//
//  Created by zhanghaifeng on 16/10/22.
//  Copyright © 2016年 BBSWaitting. All rights reserved.
//

import UIKit

extension UIButton{
    
   convenience init(setImageNamed: String, setBackgroundImage: String, target: Any?, action: Selector){
        self.init();
        // 按钮添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        // 设置button 的图片
        self.setImage(UIImage(named: setImageNamed), for: UIControlState.normal)
        self.setImage(UIImage(named: "\(setImageNamed)_highlighted"), for: UIControlState.highlighted)
        // 设置button的背景图片
        self.setBackgroundImage(UIImage(named: setBackgroundImage), for: UIControlState.normal)
        self.setBackgroundImage(UIImage(named: "\(setBackgroundImage)_highlighted"), for: UIControlState.highlighted)
        self.sizeToFit()
    
    }
    
    convenience init(setHighLightImageNamed: String? = nil, title: String? = nil, target: Any?, action: Selector){
        self.init()
        // 添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        if let img = setHighLightImageNamed{
            // 设置img
            self.setImage(UIImage(named: img), for: UIControlState.normal)
            self.setImage(UIImage(named: "\(img)_highlighted"), for: UIControlState.highlighted)
        }
        
        if let tit = title{
            // 设置title
            self.setTitle(tit, for: UIControlState.normal)
            self.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
            self.setTitleColor(XLTHENECOLOR, for: UIControlState.highlighted)
            titleLabel?.font = UIFont.systemFont(ofSize: 14)
        }
        self.sizeToFit()
    }
    
    convenience init(setBackgroundImageName: String, title:  String, fontSize: CGFloat, titleColor: UIColor,  target: Any?, action: Selector) {
        self.init()
        // 添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        self.setBackgroundImage(UIImage(named: setBackgroundImageName), for: UIControlState.normal)
        self.setTitle(title, for: UIControlState.normal)
        self.setTitleColor(titleColor, for: UIControlState.normal)
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        self.sizeToFit()
    }
    
    
}
